package com.yohann.boot.config;

import com.alibaba.fastjson.JSON;
import com.yohann.boot.common.annotation.Cache30Minutes;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author Yeung Wah
 * @since 2022/3/30
 */
@Component(Cache30Minutes.KEY_GENERATOR)
public class Cache30MinutesKeyGenerator implements KeyGenerator {

    @Override
    public Object generate(Object target, Method method, Object... objects) {
        String className = target.getClass().getName();
        String methodName = method.getName();
        String encode = MD5Encoder.encode(JSON.toJSONString(objects).getBytes());

        //类名:方法名:方法入参
        return String.join(":", className, methodName, encode);
    }

}